package com.may.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import com.may.config.DataSources;
import com.may.config.DynamicRoutingDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by Youjie on 2017/7/23.
 */
@Configuration
@EnableTransactionManagement
public class DataSourceConfig {

    @Bean
    @ConfigurationProperties(prefix = "datasource.db_a")
    @Qualifier("dataSourceDefault")
    public DataSource dataSourceDefault(){
         return new DruidDataSource();
    }

    @Bean
    @ConfigurationProperties(prefix = "datasource.db_b")
    @Qualifier("dataSourceB")
    public DataSource dataSourceB(){
        return new DruidDataSource();
    }
    
    @Bean
    @ConfigurationProperties(prefix = "datasource.db_c")
    @Qualifier("dataSourceC")
    public DataSource dataSourceC(){
        return new DruidDataSource();
    }

    /**
     <bean id="multipleDataSource"
                 class="com.hxqc.basic.dependency.datasource.ThreadLocalRountingDataSource">
         <property name="defaultTargetDataSource" ref="ncDataSource"/>
         <property name="targetDataSources">
            <map key-type="com.hxqc.basic.dependency.datasource.DataSources">
                 <entry key="DATASOURCE" value-ref="erpDataSource"/>
                 <entry key="DATASOURCE_DEFAULT" value-ref="ncDataSource"/>
                 <entry key="DATASOURCE_ORACLE" value-ref="oracleDataSource"/>
            </map>
         </property>
     </bean>

     */
    @Bean
    @Primary
    public DataSource multipleDataSource(){
        DynamicRoutingDataSource dynamicDataSource = new DynamicRoutingDataSource();
        dynamicDataSource.setDefaultTargetDataSource(dataSourceDefault());

        Map<Object,Object> dataSourcesType = new HashMap();
        dataSourcesType.put(DataSources.DATASOURCE_A,dataSourceDefault());
        dataSourcesType.put(DataSources.DATASOURCE_B,dataSourceB());
        dataSourcesType.put(DataSources.DATASOURCE_C,dataSourceC());

        dynamicDataSource.setTargetDataSources(dataSourcesType);

        return dynamicDataSource;
    }



    @Bean
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(multipleDataSource());
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:mapper/*.xml"));
        sqlSessionFactoryBean.setTypeAliasesPackage("com.may.model");
        sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("configuration.xml"));
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(multipleDataSource());
    }



}
